﻿using Himp.Platform.Dc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform.Dc
{
    /// <summary>
    /// 药品目录
    /// </summary>
    public class DrugCatalogService : DcAppService
    {
        private readonly IPublicItemService _publicItemService;
        private readonly IRepository<DrugCatalog, Guid> _drugCatalogRepository;
        private readonly IRepository<DrugBasInfo, Guid> _drugBasInfoRepository;

        public DrugCatalogService(IRepository<DrugCatalog, Guid> drugCatalogRepository
            , IRepository<DrugBasInfo, Guid> drugBasInfoRepository
            , IPublicItemService publicItemService)
        {
            _drugCatalogRepository = drugCatalogRepository;
            _publicItemService = publicItemService;
            _drugBasInfoRepository = drugBasInfoRepository;
        }

        /// <summary>
        /// 获取药品基本信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<DrugCatalogDto>> GetListAsync(DrugCatalogInput input)
        {
            var iQuery = await _drugCatalogRepository.GetQueryableAsync();

            if (input.SkipCount == 0)
            {
                var itemsDto = ObjectMapper.Map<List<DrugCatalog>, List<DrugCatalogDto>>(iQuery.ToList());
                return new PagedResultDto<DrugCatalogDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var items = iQuery.Page(input.SkipCount, input.MaxResultCount);
                var itemsDto = ObjectMapper.Map<List<DrugCatalog>, List<DrugCatalogDto>>(items.ToList());
                return new PagedResultDto<DrugCatalogDto>(itemsDto.Count, itemsDto);
            }
        }

        /// <summary>
        /// 创建药品基本信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<DrugCatalogDto> PostAsync(DrugCatalogCreateDto input)
        {
            var drugCatalog = ObjectMapper.Map<DrugCatalogCreateDto, DrugCatalog>(input);
            drugCatalog = await _drugCatalogRepository.InsertAsync(drugCatalog);
            var drugBaseInfoDto = ObjectMapper.Map<DrugCatalog, DrugCatalogDto>(drugCatalog);
            var drugBasInfo = await _drugBasInfoRepository.GetAsync(input.DrugBasInfoId);

            PubItemCreateDto pubItemCreateDto = new PubItemCreateDto();
            pubItemCreateDto.PubItemTypeId = drugBasInfo.DrugType.PubItemTypeId;
            pubItemCreateDto.DefChrgitmTypeId = drugBasInfo.DefChrgitmTypeId;

            pubItemCreateDto.PubItemCodg = input.DrugCodg;
            pubItemCreateDto.PubItemName = drugBasInfo.DrugName;
            pubItemCreateDto.Spec = input.DrugSpec;
            pubItemCreateDto.Unt = input.MinUntName;
            pubItemCreateDto.Pric = input.RtalPric;
            pubItemCreateDto.DetlItemId = drugCatalog.Id;
            
            await _publicItemService.PostAsync(pubItemCreateDto);

            return drugBaseInfoDto;
        }

        /// <summary>
        /// 更新药品基本信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<DrugCatalogDto> PutAsync(Guid id, DrugCatalogUpdateDto input)
        {
            var drugCatalog = await _drugCatalogRepository.GetAsync(id);
            drugCatalog = ObjectMapper.Map(input, drugCatalog);
            drugCatalog = await _drugCatalogRepository.UpdateAsync(drugCatalog);
            var drugBaseInfoDto = ObjectMapper.Map<DrugCatalog, DrugCatalogDto>(drugCatalog);

            return drugBaseInfoDto;
        }

        /// <summary>
        /// 删除药品基本信息
        /// </summary>
        /// <returns></returns>
        public async Task DeleteAsync(Guid id)
        {
            await _drugCatalogRepository.DeleteAsync(id);
        }
    }
}
